<?php
require './include/common.inc.php';
include_once 'type.class.php';
include_once 'form.class.php';
include_once 'member.class.php';
require 'category.class.php';
$category = new category();
$path = 'alumni/';
$category_single = $category->path_get_category($path);
$catid = $category_single['catid'];
$page_title =  $category_single['catname'] .'  --  '. $site_conf->get('SITE_NAME');
$css_file = 'chl.css';
$cat_url_tmp = explode('/', $category_single['url']);
$top_url = $cat_url_tmp[0].'/';
if($top_url == $category_single['url'])
{
    $top_catid = $catid;
    $top_category_single = $category_single;
}
else
{
    $top_category_single = $category->path_get_category($top_url);
    $top_catid = $top_category_single['catid'];
}
$type = new type();
$member=new member();
$site_id = get_current_site();
$action = @$_GET['action'];
$current_member = isset($_SESSION['current_member'])?$_SESSION['current_member']:false;

//相册分类
$phototypes = array(1=>'校园追忆',2=>'同学时光',3=>'个人风采',4=>'同城岁月');

switch ($action)
{
    //交流园地
    case 'jlyd':

        $member_ly = new member_ly();
        $page = isset($_GET['page']) ? max(1,intval($_GET['page'])) : 1;
        $pagesize = 10;
        $tp = isset($tp) ? $tp : 'ly1';
        $my_ly_list = $member_ly->get_ly(array('status'=>STATUS_NORMAL,'type'=>$tp), $page, $pagesize);
        $pages = $member_ly->pages;
        $typelist = $type->get_typs('ly');
        include template('default', 'member_jlyd');
        exit;


    //校友相册
    case 'xyxc':

        ini_set ('memory_limit', '256M');
        $page = isset($_GET['page']) ? max(1,intval($_GET['page'])) : 1;
        $pagesize = 12;

        $member_photo = new member_photo();
        $tp = @empty($phototypes[$tp]) ? 1 : $tp;
        $parms = array('status' => STATUS_NORMAL,'phototype'=>$tp);
        $data = $member_photo->get_photo($parms, $page, $pagesize);
        $pages = $member_photo->pages;
        include template('default', 'member_xyxc');
        exit;
}

if($action!='login' && $action!='reg' && $current_member==false)
{
    if(isset($edufepm) && $edufepm=='iframe')
	{
		$response->alert('请您登陆后再访问');
		$response->redirect($base_dir.'member.php?action=login');
		$response->real_execute();
	}
	else
	{
		showmessage('请您登陆后再访问',$base_dir.'member.php?action=login');
	}
}
if(isset($mid))
{
    $self = $mid == $_SESSION['current_member']['mid'] ? true : false;
    $info = $member->get_member_by_id($mid);
}
else
{
    $self = true;
    $info = @$member->get_member_by_id($_SESSION['current_member']['mid']);
}

switch($action)
{
    //登录
    case 'login':
        if(isset ($_POST['email']))
        {
            if(empty($email))
            {
                $response->alert('请输入email');
                $response->execute();
            }
            if(empty($password))
            {
                $response->alert('请输入密码');
                $response->execute();
            }
            $info = $member->get_member_by_email($email);
            if(!isset($info['email']) || $info['status']!=99)
            {
                $response->alert('您输入的email不存在');
                $response->execute();
            }
            if(md5($password) != $info['password'] )
            {
                $response->alert('密码不正确');
                $response->execute();
            }
            $_SESSION['current_member'] = $info;
            $response->alert('登录成功');
            $response->redirect('/member.php');
            $response->execute();
        }
        else
        {
            include template('default', 'login');
        }
        break;
        
    //注册
    case 'reg';
        if(isset ($_POST['email']))
        {
            if(empty($email))
            {
                $response->alert('请输入email');
                $response->execute();
            }
            if(empty($password))
            {
                $response->alert('请输入密码');
                $response->execute();
            }
            if(empty($truename))
            {
                $response->alert('请输入真实姓名');
                $response->execute();
            }
            if(empty($types[1]))
            {
                $response->alert('请选择班级');
                $response->execute();
            }
            if($password != $repassword )
            {
                $response->alert('密码不一致');
                $response->execute();
            }
            $info = $member->get_member_by_email( $email );
            if( isset($info['mid']) )
            {
                $response->alert( '邮箱已存在,请重新输入' );
            }
            $params = array(
				'email'         =>  $email,
				'password'      =>  md5( $password ),
                'truename'      =>  $truename,
                'tel'           =>  $tel,
                'department'    =>  $department,
                'major'         =>  $types[0],
                'class'         =>  $types[1],
                'status'        =>  1,
                'addtime'       =>  time(),
                'lastlogin'     =>  0
			);
			$res = $member->add_member( $params );
            if( $res)
			{
				$response->alert( '申请成功，请等待审核成功后再登录' );
				$response->redirect( '/alumni/' );
			}
			else
			{
				$response->alert( '申请失败' );
			}
			$response->execute();
            
        }
        else
        {
            include template('default', 'reg');
        }
        break; 
        
    //信息
    case 'message':
        
        $member = new member();
        $member_message = new member_message();
        $mid = $_SESSION['current_member']['mid'];
        $params = array('touser'   =>  $mid);
        $page = isset($_GET['page']) ? max(1,intval($_GET['page'])) : 1;
        $pagesize = 10;
        $data = $member_message->get_message($params, $page, $pagesize);
        $data_fromme = $member_message->get_message(array('fromuser'=>$mid), $page, $pagesize);
        include template('default', 'member_message');
        break;
    
    case 'add_message':
        
        if(!isset($touser))
        {
            showmessage('请指定发送给谁');
        }
        if(!empty($_POST))
        {
            $member_message = new member_message();
            $data['fromuser'] = $_SESSION['current_member']['mid'];
            $data['touser'] = $touser;
            $data['content'] = $content;
            $data['addtime'] = TIME;
            $member_message->add_message($data);
            $response->redirect('/member.php?action=message');
            $response->execute();
        }
        else
        {
            include template('default', 'member_add_message');
        }
        break;
    
    case 'del_message':
        
        $id = $_GET['id'];
        $mid = $_SESSION['current_member']['mid'];
        $member_message = new member_message();
        $info = $member_message->get_message_by_id($id);
        if($mid != $info['touser'])
        {
            $response->alert('不是您的信息，您不能删除');
            $response->execute();
        }
        $res = $member_message->delete_message($id);
        if($res == true)
        {
            $response->alert('删除成功');
            $response->script('self.location=self.location');
        }
        else
        {
            $response->alert('删除失败');
        }
        $response->execute();
        break;
    
    //留言列表
    case 'ly':
        $member_ly = new member_ly();
        $mid = $self ? $_SESSION['current_member']['mid'] : $mid;
        $params = array('mid'   =>  $mid);
        $page = isset($_GET['page']) ? max(1,intval($_GET['page'])) : 1;
        $pagesize = 10;
        $my_ly_list = $member_ly->get_ly($params, $page, $pagesize);
        $pages = $member_ly->pages;
        $typelist = $type->get_typs('ly');
        include template('default', 'member_ly');
        break;
    
    //删除留言
    case 'del_ly':
        $id = $_GET['id'];
        $mid = $_SESSION['current_member']['mid'];
        $member_ly = new member_ly();
        $info = $member_ly->get_ly_by_id($id);
        if($mid != $info['mid'])
        {
            $response->alert('不是您的留言，您不能删除');
            $response->execute();
        }
        $res = $member_ly->delete_ly($id);
        if($res == true)
        {
            $response->alert('删除成功');
            $response->script('self.location=self.location');
        }
        else
        {
            $response->alert('删除失败');
        }
        $response->execute();
        break;
        
    //添加留言
    case 'add_ly':
        if(isset($_POST['content']))
        {
            if(empty($_POST['content']))
            {
                $response->alert('请输入留言内容');
                $response->execute();
            }
            $member_ly = new member_ly();
            $params = array(
                'content'   =>  $_POST['content'],
                'type'      =>  '',
                'reply'     =>  '',
                'status'    =>  1,
                'addtime'   =>  time(),
                'replytime' =>  0,
                'mid'       =>  $_SESSION['current_member']['mid'],
                'title'     =>  ''
            );
            $res = $member_ly->add_ly($params);
            if($res == false)
            {
                $response->alert('发表失败');
            }
            else
            {
                $response->alert('发表成功');
                $response->redirect('/member.php?action=ly');
            }
            $response->execute();
        }
        else
        {
            include template('default', 'member_add_ly');
        }
        break;

    
    //添加图片
    case 'add_photo':
        
        if(!empty($_POST))
        {
            $member_photo = new member_photo();
            $data['mid'] = $_SESSION['current_member']['mid'];
            $data['addtime'] = TIME;
            $member_photo->add_photo($data);
            $response->redirect('/member.php?action=photo');
            $response->execute();
        }
        else
        {
            include template('default', 'member_add_photo');
        }
        break;
        
    //图片列表
    case 'photo':

        ini_set ('memory_limit', '256M');
        $mid = isset($mid) ? $mid : $_SESSION['current_member']['mid'];
        
        $self = false;
        if($mid == $_SESSION['current_member']['mid'])
        {
            $self = true;
        }
        $page = isset($_GET['page']) ? max(1,intval($_GET['page'])) : 1;
        $pagesize = 12;
        
        $member_photo = new member_photo();
        $parms = array('mid' => $mid);
        $data = $member_photo->get_photo($parms, $page, $pagesize);
        $pages = $member_photo->pages;
        include template('default', 'member_photo');
        break;
    //删除图片
    case 'del_photo':
        
        if(!isset($id)) $response->alert('没有找到图片');
        $mid = $_SESSION['current_member']['mid'];
        $member_photo = new member_photo();
        $info = $member_photo->get_photo_by_id($id);
        if($info['mid'] != $mid)
        {
            $response->alert('不是您的图片，您不能删除');
            $response->execute();
        }
        $res = $member_photo->delete_photo($id);
        if($res == true)
        {
            $response->alert('删除成功');
            $response->script('self.location=self.location');
        }
        else
        {
            $response->alert('删除失败');
        }
        $response->execute();
        break;

    //查找同同班同学
    case 'classmates':

        $page = isset($_GET['page']) ? max(1,intval($_GET['page'])) : 1;
        $pagesize = 10;

        $member_info = $member->get_member_by_id($_SESSION['current_member']['mid']);
        $class = $member_info['class'];
        $major = $member_info['major'];


        $parm = array('class'=>$class, 'major'=>$major, 'status'=>STATUS_NORMAL);
        $data = $member->get_members($parm, $page, $pagesize);
        $pages = $member->pages;
        include template('default', 'member_classmates');
        break;


    //修改个人资料
    default;

        if(!isset($mid)) $mid = $_SESSION['current_member']['mid'];
        $info = $member->get_member_by_id($mid);
        if(isset($_POST['email']))
        {
            $info = $member->get_member_by_id( $info['mid'] );//获取数据库中该用户的所有信息
            if( $info['email'] != $email )//如果修改了用户名，则检查数据库中用户是否存在
            {
                $check_res = $member->get_member_by_email( $email );
                if( isset($check_res['mid']) )
                {
                    $response->alert( 'email存在,请重新输入' );
                    $response->execute();
                }
            }
			if( empty( $email ) )
			{
				$response->alert( 'email不能为空' );
                $response->execute();
			}
            
            if( !empty( $password ) )
            {
                if(md5($oldpassword) != $info['password'])
                {
                    $response->alert('当前密码不正确');
                    $response->execute();
                }
                if( $password != $repassword )
                {
                    $response->alert( '确认密码与设定密码不符,请重新确认' );
                    $response->execute();
                }
                $upd_password = md5( $password );
            }
            else
            {
                $upd_password = $info['password'];
            }
			
			$params = array(
				'email'         =>  $email,
				'truename'         =>  $truename,
				'birthday'         =>  $birthday,
				'interest'         =>  $interest,
				'addr'          =>  $addr,
				'password'      => $upd_password,
                'tel'           =>  $tel,
                'department'    =>  $department,
                'photo'         =>  $photo,
                'major'         =>  $types[0],
                'class'         =>  $types[1],
			);
			$res = $member->edit_member( $params, $info['mid'] );

			if( !empty($res) )
			{
				$response->alert( '修改成功' );
				$response->script( 'self.location = self.location' );
			}
			else
			{
				$response->alert( '修改失败' );
			}
			$response->execute();
        }
        else
        {
            if(isset($_GET['mid']))
            {
                if($_GET['mid'] != $_SESSION['current_member']['mid'])
                {
                    $info = $member->get_member_by_id($_GET['mid']);
                    $self = false;
                }
            }
            $major_types = $type->get_typs('major');
            include template('default', 'member_index');
        }
        break;
}
?>
